var host = 'localhost';
//var host = 'www.schaakliga-antwerpen.info';
//var host = 'test.schaakliga-antwerpen.info';
//var host = 'www.schaakliga-antwerpen-info.nl03.members.pcextreme.nl';

var req;
var rowNr;
var playerList;
var club;
var board;
var department;

function isNumeric(x) {
	// I use this function like this: if (isNumeric(myVar)) { }
	// regular expression that validates a value is numeric
	var RegExp = /^(-)?(\d*)(\.?)(\d*)$/; // Note: this WILL allow a number that ends in a decimal: -452.
	// compare the argument to the RegEx
	// the 'match' function returns 0 if the value didn't match
	var result = x.match(RegExp);
	return result;
}

function loadXMLDoc(url, functionName) {
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();        
        req.onreadystatechange = functionName;
        req.open("GET", url, true);
        req.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = functionName;
            req.open("GET", url, true);
            req.send();
        }
    }
}

function processReqChange() {
    // only if req shows "complete"
    if (req.readyState == 4) {
        // only if "OK"
       try {
        if (req.status == 200) {
            // ...processing statements go here...
			response = req.responseXML.documentElement;
	      	method = response.getElementsByTagName('method')[0].firstChild.data;
	      	result = response.getElementsByTagName('result')[0].firstChild.data;
	      	eval(method + '(\'\', result)');
        }
      } catch(e) {
         //alert("Een fout heeft zich voorgedaan :" + e);
      }
    }
}

function processReqPlayer() {
    // only if req shows "complete"
    if (req.readyState == 4) {
        // only if "OK"
        try {
        if (req.status == 200) {
            // ...processing statements go here...
			response = req.responseXML.documentElement;
            method = response.getElementsByTagName('method')[0].firstChild.data;
	      	name = response.getElementsByTagName('name')[0].firstChild.data;
	      	elo = response.getElementsByTagName('elo')[0].firstChild.data;
	      	club = response.getElementsByTagName('club')[0].firstChild.data;
	      	eval(method + '(\'\', rowNr, name, elo, club)');
        }
        } catch(e) {
          eval(method + '(\'\', rowNr, \'notfound\', \'\', \'\')');
          //alert("Een fout heeft zich voorgedaan :" + e);
      }
    }
}

function processReqPlayerRow() {
    // only if req shows "complete"
    if (req.readyState == 4) {
        // only if "OK"
        try {
        if (req.status == 200) {
            // ...processing statements go here...
			response = req.responseXML.documentElement;
	      	method = response.getElementsByTagName('method')[0].firstChild.data;
	      	result = response.getElementsByTagName('result')[0].firstChild.data;
	      	eval(method + '(\'\', rowNr, result)');
        }
        }
      catch(e) {
      	//alert("Een fout heeft zich voorgedaan :" + e);
      }
    }
}

function processReqPlayerTeamRow() {
    // only if req shows "complete"
    if (req.readyState == 4) {
        // only if "OK"
        try {
        if (req.status == 200) {
            // ...processing statements go here...
			response = req.responseXML.documentElement;
	      	method = response.getElementsByTagName('method')[0].firstChild.data;
	      	result = response.getElementsByTagName('result')[0].firstChild.data;
	      	eval(method + '(\'\', playerList, club, rowNr, result)');
        }
        }
      catch(e) {
      	//alert("Een fout heeft zich voorgedaan :" + e);
      }
    }
}

function processReqClubTeam() {
    // only if req shows "complete"
    if (req.readyState == 4) {
        // only if "OK"
        try {
        	if (req.status == 200) {
            // ...processing statements go here...
			response = req.responseXML.documentElement;
	      	method = response.getElementsByTagName('method')[0].firstChild.data;
	      	result = response.getElementsByTagName('result')[0].firstChild.data;
	      	eval(method + '(\'\', clubNr, department, result)');
        }
        }
      catch(e) {
         //alert("Een fout heeft zich voorgedaan :" + e);
      }
    }
}

function processReqComment() {
    // only if req shows "complete"
    if (req.readyState == 4) {
        // only if "OK"
        try {
        	if (req.status == 200) {
            // ...processing statements go here...
			response = req.responseXML.documentElement;
	      	method = response.getElementsByTagName('method')[0].firstChild.data;
	      	result = response.getElementsByTagName('result')[0].firstChild.data;
	      	eval(method + '(\'\', playerList, result)');
        }
        }
      catch(e) {
          //alert("Een fout heeft zich voorgedaan :" + e);
      }
    }
}

function processReqClubComment() {
    // only if req shows "complete"
    if (req.readyState == 4) {
        // only if "OK"
        try {
        	if (req.status == 200) {
            // ...processing statements go here...
			response = req.responseXML.documentElement;
	      	method = response.getElementsByTagName('method')[0].firstChild.data;
	      	result = response.getElementsByTagName('result')[0].firstChild.data;
	      	eval(method + '(\'\', clubNr, result)');
        }
        }
      catch(e) {
          //alert("Een fout heeft zich voorgedaan :" + e);
      }
    }
}

function processReqStoreResult() {
    // only if req shows "complete"
    if (req.readyState == 4) {
        // only if "OK"
        try {
        	if (req.status == 200) {
            // ...processing statements go here...
			response = req.responseXML.documentElement;
	      	method = response.getElementsByTagName('method')[0].firstChild.data;
	      	result = response.getElementsByTagName('result')[0].firstChild.data;
	      	eval(method + '(\'\', board, result)');
        }
        }
      catch(e) {
          //alert("Een fout heeft zich voorgedaan :" + e);
      }
    }
}

function processReqTeam() {
    // only if req shows "complete"
    if (req.readyState == 4) {
        // only if "OK"
        try {
        if (req.status == 200) {
            // ...processing statements go here...
    			response = req.responseXML.documentElement;
	      	method = response.getElementsByTagName('method')[0].firstChild.data;
	      	teamId = response.getElementsByTagName('teamId')[0].firstChild.data;
	      	teamName = response.getElementsByTagName('teamName')[0].firstChild.data;
	      	eval(method + '(\'\', teamId, teamName, result)');
        }
        } catch(e) {
          //alert("Een fout heeft zich voorgedaan :" + e);
      }
    }
}

function processReqChangeTeamName() {
    // only if req shows "complete"
    if (req.readyState == 4) {
        // only if "OK"
        try {
	        if (req.status == 200) {
	            // ...processing statements go here...
	    		response = req.responseXML.documentElement;
		      	method = response.getElementsByTagName('method')[0].firstChild.data;
		      	teamId = response.getElementsByTagName('teamId')[0].firstChild.data;
		      	result = response.getElementsByTagName('result')[0].firstChild.data;
		      	eval(method + '(\'\', teamId, result)');
	        }
        } catch(e) {
          //alert("Een fout heeft zich voorgedaan :" + e);
    	}
    }
}

function checkKey(input, response) {
	if (response != '') {
    	// Response mode
    	message = document.getElementById('noKeyFound');
    	if (response == 1) {
      		message.className = 'hidden';
    	} else {
      		message.className = 'error';
		}
	} else{
    	// Input mode
    	url = 'http://' + host + '/xml/checkPlayerKey.php?q=' + input;
    	loadXMLDoc(url, processReqChange);
  }

}

function getPlayer(input, row, name, elo, club) {
	if (name != '') {
    	// Response mode
        var nameField = document.getElementById('playerName_' + row);
        var eloField = document.getElementById('playerElo_' + row);
        var clubField = document.getElementById('playerClub_' + row);
        
        if (nameField != null) {
	        if (name != 'notfound') {
	        	nameField.className = 'default';
	        	nameField.innerHTML = '&nbsp;&nbsp;' + name;
	        } else {
	        	nameField.className = 'error';
	        	nameField.innerHTML = '&nbsp;&nbsp;Niet gevonden';
	        }
        }
        
        if (eloField != null) {
        	eloField.className = 'default';
        	eloField.innerHTML = elo;
        }
        
        if (clubField != null) {
        	clubField.className = 'default';
        	clubField.innerHTML = club;
        }
        
    	/*
    	var playerListForm = document.getElementById('playerListForm');
    	var elements = playerListForm.elements;
		elements['playerName_' + row].value = '  ' + name;
		elements['playerElo_' + row].value = elo;
		elements['playerClub_' + row].value = club;
		*/
	} else {
    	// Input mode
    	rowNr = row;
    	url = 'http://' + host + '/xml/getPlayer.php?playerId=' + input + '&row=' + row;
    	loadXMLDoc(url, processReqPlayer);
  }

}

function storePlayerTeam(input, playerListId, clubId, row, response) {
	if (response != '') {
    	// Response mode
    	message = document.getElementById('noValidTeam');
    	if (response == 1) {
      		message.className = 'hidden';
    	} else {
      		message.className = 'error';
      		message.innerHTML = response;
		}
	} else {
    	// Input mode
      	rowNr = row;
      	playerList = playerListId;
      	club = clubId;
        url = 'http://' + host + '/xml/storePlayerTeam.php?teamNr=' + input + '&clubId=' + clubId + '&row=' + row;
      	if (playerListId != '') {
            url = 'http://' + host + '/xml/storePlayerTeam.php?teamNr=' + input + '&playerListId=' + playerListId
        	+ '&clubId=' + clubId + '&row=' + row;
        }
        loadXMLDoc(url, processReqPlayerTeamRow);
  }

}

function storePlayerName(input, row, response) {
	if (response != '') {
    	// Response mode
    	message = document.getElementById('playerNameIsFixed');
    	errMessage = document.getElementById('playerNameStoreError');
    	if (response == 1) {
      		message.className = 'hidden';
      		errMessage.className = 'hidden';
    	} else if (response == 2) {
            message.className = 'error';
            errMessage.className = 'hidden';
        } else {
      		message.className = 'hidden';
      		errMessage.className = 'error';
		}
	} else {
    	// Input mode
      	rowNr = row;
        url = 'http://' + host + '/xml/storePlayerName.php?playerName=' + input + '&row=' + row;
        loadXMLDoc(url, processReqPlayerRow);
  }

}

function storePlayerElo(input, row, response) {
	if (response != '') {
    	// Response mode
    	message = document.getElementById('playerEloIsInvalid');
    	message2 = document.getElementById('playerEloIsFixed');
    	if (response == 1) {
      		message.className = 'hidden';
      		message2.className = 'hidden';
    	} else if (response == 2) {
            message.className = 'hidden';
      		message2.className = 'error';
		} else {
            message.className = 'error';
      		message2.className = 'hidden';
        }
	} else {
    	// Input mode
      	rowNr = row;
        url = 'http://' + host + '/xml/storePlayerElo.php?playerElo=' + input + '&row=' + row;
        loadXMLDoc(url, processReqPlayerRow);
  }

}

function storePlayerClub(input, row, response) {
	if (response != '') {
    	// Response mode
    	message = document.getElementById('playerClubIsInvalid');
    	message2 = document.getElementById('playerClubIsFixed');
        if (response == 1) {
      		message.className = 'hidden';
      		message2.className = 'hidden';
    	} else if (response == 2) {
            message.className = 'hidden';
      		message2.className = 'error';
		} else {
            message.className = 'error';
      		message2.className = 'hidden';
        }
	} else {
    	// Input mode
      	rowNr = row;
        url = 'http://' + host + '/xml/storePlayerClub.php?playerClub=' + input + '&row=' + row;
        loadXMLDoc(url, processReqPlayerRow);
  }

}

function storeClubTeams(input, clubId, departmentNr, response) {
	if (response != '') {
    	// Response mode
      message = document.getElementById('clubTeamNotStored');
  	  if (response != 1) {
    	  message.className = 'error';
        message.innerHTML = response;
  	  }
	} else {
    	// Input mode
    	clubNr = clubId;
    	department = departmentNr;
    	url = 'http://' + host + '/xml/storeClubTeams.php?clubId=' + clubId + '&departmentNr=' + departmentNr + '&nrOfTeams=' + input ;
    	loadXMLDoc(url, processReqClubTeam);
  }

}

function storeComment(input, playerListId, response) {
	if (response != '') {
    	// Response mode
  	  if (response != 1) {
    	message = document.getElementById('playerListCommentNotStored');
        message.className = 'error';
        message.innerHTML = response;
  	  }
	} else {
    	// Input mode
    	playerList = playerListId;
    	url = 'http://' + host + '/xml/storeComment.php?comment=' + input +
    		'&playerListId=' + playerListId;
    	loadXMLDoc(url, processReqChange);
    }
}

function storeClubComment(input, clubId, response) {
	if (response != '') {
    	// Response mode
	} else {
    	// Input mode
    	clubNr = clubId;
    	url = 'http://' + host + '/xml/storeClubComment.php?comment=' + input + '&clubId=' + clubId;
    	loadXMLDoc(url, processReqClubComment);
  }

}

function storeDepartment(input, response) {
	if (response != '') {
    	// Response mode
	} else {
    	// Input mode
        url = 'http://' + host + '/xml/storeDepartment.php?dep=' + input;
    	loadXMLDoc(url, processReqChange);
    }
}

function storeRound(input, response) {
	if (response != '') {
    	// Response mode
	} else {
    	// Input mode
    	url = 'http://' + host + '/xml/storeRound.php?round=' + input;
    	loadXMLDoc(url, processReqChange);
    }
}

function storeHomeTeam(input, response) {
	if (response != '') {
    	// Response mode
	} else {
    	// Input mode
    	url = 'http://' + host + '/xml/storeHomeTeam.php?homeTeam=' + input;
    	loadXMLDoc(url, processReqChange);
    }
}

function storeVisitTeam(input, response) {
	if (response != '') {
      // Response mode
      message = document.getElementById('gameDataAlreadyReceived');
	  
      if (response == 2) {
        	var sendResultsForm = document.getElementById('insert_result');
			var elements = sendResultsForm.elements;
			var submit = elements['sendResult'];
			submit.disabled = 'true';
        	message.className = 'default';
  		} else {
  			//elements['sendResult'].disabled = false;
    		message.className = 'hidden';
  		}
	} else {
    	// Input mode
    	url = 'http://' + host + '/xml/storeVisitTeam.php?visitTeam=' + input;
    	loadXMLDoc(url, processReqChange);
    }
}

function enableSubmitIfPossible() {

	var sendResultsForm = document.getElementById('insert_result');
	var elements = sendResultsForm.elements;
		
	var submit = elements['sendResult'];
	var department = elements['department'];
  	var round = elements['round'];
  	var homeTeam = elements['homeTeam'];
  	var visitTeam = elements['visitTeam'];
  	var homePlayerRank_1 = elements['homePlayerRank_1'];
  	var homePlayerRank_2 = elements['homePlayerRank_2'];
  	var homePlayerRank_3 = elements['homePlayerRank_3'];
  	var homePlayerRank_4 = elements['homePlayerRank_4'];
  	var homePlayerRank_5 = elements['homePlayerRank_5'];
  	var homePlayerRank_6 = elements['homePlayerRank_6'];
  	var visitPlayerRank_1 = elements['visitPlayerRank_1'];
  	var visitPlayerRank_2 = elements['visitPlayerRank_2'];
  	var visitPlayerRank_3 = elements['visitPlayerRank_3'];
  	var visitPlayerRank_4 = elements['visitPlayerRank_4'];
  	var visitPlayerRank_5 = elements['visitPlayerRank_5'];
  	var visitPlayerRank_6 = elements['visitPlayerRank_6'];
  	var result_1 = elements['result_1'];
  	var result_2 = elements['result_2'];
  	var result_3 = elements['result_3'];
  	var result_4 = elements['result_4'];
  	var result_5 = elements['result_5'];
  	var result_6 = elements['result_6'];
    
    
  	
  	// bypassing IE problem wich accessing dojo elements by name
  	for(i=0; i<elements.length; i++) {
		if (elements[i].name == 'homeTeam') {
			homeTeam = elements[i];
		}
		if (elements[i].name == 'visitTeam') {
			visitTeam = elements[i];
		}
	}
  	
  	submit.disabled = true;
  	
  	var depUpCase = department.value.toUpperCase();
    
	if (depUpCase == '1') {
		if (round.value != '' && homeTeam.value != '' 
     		&& visitTeam.value != ''
     		&& result_1.value != ''
     		&& result_2.value != ''
     		&& result_3.value != ''
     		&& result_4.value != ''
    		&& result_5.value != ''
    		&& result_6.value != '') {
            

     		submit.disabled = false;
     			
     	}
      	
      } else if (depUpCase == '2A' 
      	|| depUpCase == '2B' 
      	|| depUpCase == '3A' 
      	|| depUpCase == '3B') {
      		
      	if (round.value != '' && homeTeam.value != '' 
      		&& visitTeam.value != ''
      		&& result_1.value != ''
      		&& result_2.value != ''
      		&& result_3.value != ''
      		&& result_4.value != '') {
      		
      		submit.disabled = false;
      	}
        
        
      		
	}

}

function storeResult(input, boardNr, response) {
	if (response != '') {
    	// Response mode
        
    	if (response != 0) {
    		var resultField = document.getElementById('currentResult');
    		resultField.className = 'default';
        	resultField.innerHTML = response;
        	
        	enableSubmitIfPossible();
        	
    	}
	} else {
    	// Input mode
    	board = boardNr;
    	url = 'http://' + host + '/xml/storeResult.php?result=' + input + '&boardNr=' + boardNr;
    	loadXMLDoc(url, processReqStoreResult);
    }
}

function processReqPlayerByRank() {
    // only if req shows "complete"
    if (req.readyState == 4) {
        // only if "OK"
        try {
          if (req.status == 200) {
            // ...processing statements go here...
      			response = req.responseXML.documentElement;
  	      	method = response.getElementsByTagName('method')[0].firstChild.data;
  	      	name = response.getElementsByTagName('name')[0].firstChild.data;
  	      	key = response.getElementsByTagName('key')[0].firstChild.data;
  	      	error = response.getElementsByTagName('error')[0].firstChild.data;
  	      	eval(method + '(\'\', rowNr, key, name, error)');
          }
        } catch(e) {
          //alert("Een fout heeft zich voorgedaan :" + e);
      }
    }
}

function changeTeamName(input, teamId, response) {
	if (response != '') {
    	// Response mode
	} else {
    	// Input mode
    	url = 'http://' + host + '/xml/changeTeamName.php?name=' + input + '&teamId=' + teamId;
    	loadXMLDoc(url, processReqChangeTeamName);
    }
}

function getHomePlayerByRank(input, row, key, name, error) {
	if (key != '') {
    	// Response mode
    	var keyField = document.getElementById('homePlayerId_' + row);
  		var nameField = document.getElementById('homePlayerName_' + row);
  		var errorField = document.getElementById('errorsHome');
     
    	if (key != null) {
   			keyField.className = 'default';
        	keyField.innerHTML = key;
    		nameField.className = 'default';
        	nameField.innerHTML = name;
   			if (error != '' && error != '0') {
	    		errorField.className = 'error';
	        	errorField.innerHTML = error;
        	} else {
        		errorField.className = 'hidden';
	        	errorField.innerHTML = '';
        	}
        	
        	enableSubmitIfPossible();
        	
    	} else {
      		keyField.className = 'error';
        	keyField.innerHTML = '?';
    		nameField.className = 'error';
        	nameField.innerHTML = 'Speler niet gevonden';
    	}
	} else {
    	// Input mode
    	rowNr = row;
    	url = 'http://' + host + '/xml/getHomePlayerByRank.php?rank=' + input + '&row=' + row;
    	loadXMLDoc(url, processReqPlayerByRank);
  }

}

function getVisitPlayerByRank(input, row, key, name, error) {
	if (key != '') {
    	// Response mode
    	var keyField = document.getElementById('visitPlayerId_' + row);
  		var nameField = document.getElementById('visitPlayerName_' + row);
		var errorField = document.getElementById('errorsVisit');
     
    	if (key != null) {
   			keyField.className = 'default';
        	keyField.innerHTML = key;
    		nameField.className = 'default';
        	nameField.innerHTML = name;
   			if (error != '' && error != '0') {
	    		errorField.className = 'error';
	        	errorField.innerHTML = error;
        	} else {
        		errorField.className = 'hidden';
	        	errorField.innerHTML = '';
        	}
        	
        	enableSubmitIfPossible();
        	
    	} else {
      		keyField.className = 'error';
        	keyField.innerHTML = '?';
    		nameField.className = 'error';
        	nameField.innerHTML = 'Speler niet gevonden';
    	}
	} else {
    	// Input mode
    	rowNr = row;
    	url = 'http://' + host + '/xml/getVisitPlayerByRank.php?rank=' + input + '&row=' + row;
    	loadXMLDoc(url, processReqPlayerByRank);
  	}

}

